package com.example.ice.u17.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.ice.u17.R;
import com.example.ice.u17.adapter.BookShelfAdapter;

/**
 * A simple {@link Fragment} subclass.
 */
public class BookshelfFragment extends Fragment {


    private ViewPager viewPager;

    public BookshelfFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_bookshelf, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        viewPager = ((ViewPager) view.findViewById(R.id.vp));
        final RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.rg);
        viewPager.setAdapter(new BookShelfAdapter(((AppCompatActivity) getContext()).getSupportFragmentManager()));
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < radioGroup.getChildCount(); i++) {
                    RadioButton childAt = (RadioButton) radioGroup.getChildAt(i);
                    if (position != i) {
                        childAt.setChecked(false);
                    } else {
                        childAt.setChecked(true);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb1:
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.rb2:
                        viewPager.setCurrentItem(1);
                        break;
                    case R.id.rb3:
                        viewPager.setCurrentItem(3);
                        break;
                }
            }
        });
    }
}
